<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   易用的深度学习框架Keras简介及使用  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://dataunion.org/16787.html",
                  host: "http://dataunion.org",
                  prePath: "http://dataunion.org",
                  scheme: "http",
                  pathBase: "http://dataunion.org/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466457248: Accept with keywords: (title(0.25):社区,数盟,框架,深度学习, topn(0.466666666667):初始化,社区,个数,数盟,深度学习,行业资讯,类别,数据挖掘,特征,模型,识别率,python,文档,图像,组件,可视化,方法,图片,样本,函数,卷积,文章,基础架构,图像格式,框架,数据,代码,分类,灰度,模块).-->
 </head>
 <body onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    易用的深度学习框架Keras简介及使用
   </h1>
  </div>
  <div class="container" id="xmain">
   ﻿﻿
   <title>
    易用的深度学习框架Keras简介及使用 | 数盟社区
   </title>
   <!-- All in One SEO Pack 2.2.7.6.2 by Michael Torbert of Semper Fi Web Design[32,66] -->
   <!-- /all in one seo pack -->
   <!--
<div align="center">
<a href="http://strata.oreilly.com.cn/hadoop-big-data-cn?cmp=mp-data-confreg-home-stcn16_dataunion_pc" target="_blank"><img src="http://dataunion.org/wp-content/uploads/2016/05/stratabj.jpg"/ ></a>
</div>
-->
   <header id="header-web">
    <div class="header-main">
     <hgroup class="logo">
      <h1>
       <a href="http://dataunion.org/" rel="home" title="数盟社区">
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/logo.png"/>
       </a>
      </h1>
     </hgroup>
     <!--logo-->
     <nav class="header-nav">
      <ul class="menu" id="menu-%e4%b8%bb%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-71" id="menu-item-71">
        <a href="http://dataunion.org/category/events" title="events">
         活动
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22457" id="menu-item-22457">
          <a href="http://dataunion.org/2016timeline">
           2016档期
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22459" id="menu-item-22459">
          <a href="http://dataunion.org/category/parterc">
           合作会议
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-has-children menu-item-20869" id="menu-item-20869">
        <a href="http://dataunion.org/category/tech" title="articles">
         文章
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20867" id="menu-item-20867">
          <a href="http://dataunion.org/category/tech/base" title="base">
           基础架构
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3302" id="menu-item-3302">
          <a href="http://dataunion.org/category/tech/ai" title="ai">
           人工智能
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3303" id="menu-item-3303">
          <a href="http://dataunion.org/category/tech/analysis" title="analysis">
           数据分析
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21920" id="menu-item-21920">
          <a href="http://dataunion.org/category/tech/dm">
           数据挖掘
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3314" id="menu-item-3314">
          <a href="http://dataunion.org/category/tech/viz" title="viz">
           可视化
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3305" id="menu-item-3305">
          <a href="http://dataunion.org/category/tech/devl" title="devl">
           编程语言
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-20876" id="menu-item-20876">
        <a href="http://dataunion.org/category/industry">
         行业
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16328" id="menu-item-16328">
          <a href="http://dataunion.org/category/industry/case" title="case">
           行业应用
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2112" id="menu-item-2112">
          <a href="http://dataunion.org/category/industry/demo" title="demo">
           Demo展示
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21562" id="menu-item-21562">
          <a href="http://dataunion.org/category/industry/news">
           行业资讯
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-311" id="menu-item-311">
        <a href="http://dataunion.org/category/sources" title="sources">
         资源
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20870" id="menu-item-20870">
        <a href="http://dataunion.org/category/books" title="book">
         图书
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21363" id="menu-item-21363">
        <a href="http://dataunion.org/category/training">
         课程
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-21853" id="menu-item-21853">
        <a href="http://dataunion.org/category/jobs">
         职位
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22050" id="menu-item-22050">
          <a href="http://dataunion.org/category/career">
           职业规划
          </a>
         </li>
        </ul>
       </li>
      </ul>
     </nav>
     <!--header-nav-->
    </div>
   </header>
   <!--header-web-->
   <div id="main">
    <div id="soutab">
     <form action="http://dataunion.org/" class="search" method="get">
     </form>
    </div>
    <div id="container">
     <nav id="mbx">
      当前位置：
      <a href="http://dataunion.org">
       首页
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech">
       文章
      </a>
      &gt;  正文
     </nav>
     <!--mbx-->
     <article class="content">
      <header align="centre" class="contenttitle">
       <div class="mscc">
        <h1 class="mscctitle">
         <a href="http://dataunion.org/16787.html">
          易用的深度学习框架Keras简介及使用
         </a>
        </h1>
        <address class="msccaddress ">
         <em>
          17,359 次阅读 -
         </em>
         <a href="http://dataunion.org/category/tech" rel="category tag">
          文章
         </a>
        </address>
       </div>
      </header>
      <div class="content-text">
       <p>
        之前我一直在使用Theano，前面五篇Deeplearning相关的文章也是学习Theano的一些笔记，当时已经觉得Theano用起来略显麻烦，有时想实现一个新的结构，就要花很多时间去编程，所以想过将代码模块化，方便重复使用，但因为实在太忙没有时间去做。最近发现了一个叫做Keras的框架，跟我的想法不谋而合，用起来特别简单，适合快速开发。（其实还有很多其他的深度学习框架都是比较容易用的。）
       </p>
       <h2 id="1-keras简介">
        <p name="t0">
        </p>
        1. Keras简介
       </h2>
       <p>
        Keras是基于Theano的一个深度学习框架，它的设计参考了Torch，用Python语言编写，是一个高度模块化的神经网络库，支持GPU和CPU。使用文档在这：
        <a href="http://keras.io/">
         http://keras.io/
        </a>
        ，这个框架貌似是刚刚火起来的，使用上的问题可以到github提issue:
        <a href="https://github.com/fchollet/keras">
         https://github.com/fchollet/keras
        </a>
       </p>
       <p>
        下面简单介绍一下怎么使用Keras，以Mnist数据库为例,编写一个CNN网络结构，你将会发现特别简单。
       </p>
       <h2 id="2-keras里的模块介绍">
        <p name="t1">
        </p>
        2. Keras里的模块介绍
       </h2>
       <ul>
        <li>
         <strong>
          Optimizers
         </strong>
         顾名思义，Optimizers包含了一些优化的方法，比如最基本的随机梯度下降SGD,另外还有Adagrad、Adadelta、RMSprop、Adam，一些新的方法以后也会被不断添加进来。
         <br/>
         <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
         <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57685c9c6b255437747729" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
          <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
           <span class="crayon-title">
           </span>
           <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-wrap-button" title="切换自动换行">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-expand-button" title="点击展开代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-copy-button" title="复制代码">
             <div class="crayon-button-icon">
             </div>
            </div>
            <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
             <div class="crayon-button-icon">
             </div>
            </div>
           </div>
          </div>
          <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
          </div>
          <div class="crayon-plain-wrap">
           <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
            keras&lt;span class="hljs-preprocessor"&gt;.optimizers&lt;/span&gt;&lt;span class="hljs-preprocessor"&gt;.SGD&lt;/span&gt;(lr=&lt;span class="hljs-number"&gt;0.01&lt;/span&gt;, momentum=&lt;span class="hljs-number"&gt;0.9&lt;/span&gt;, decay=&lt;span class="hljs-number"&gt;0.9&lt;/span&gt;, nesterov=False)
           </textarea>
          </div>
          <div class="crayon-main" style="">
           <table class="crayon-table">
            <tbody>
             <tr class="crayon-row">
              <td class="crayon-nums " data-settings="show">
               <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                <div class="crayon-num" data-line="crayon-57685c9c6b255437747729-1">
                 1
                </div>
               </div>
              </td>
              <td class="crayon-code">
               <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                <div class="crayon-line" id="crayon-57685c9c6b255437747729-1">
                 <span class="crayon-v">
                  keras
                 </span>
                 <span class="crayon-o">
                  &lt;
                 </span>
                 <span class="crayon-e">
                  span
                 </span>
                 <span class="crayon-t">
                  class
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-s">
                  "hljs-preprocessor"
                 </span>
                 <span class="crayon-o">
                  &gt;
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  optimizers
                 </span>
                 <span class="crayon-o">
                  &lt;
                 </span>
                 <span class="crayon-o">
                  /
                 </span>
                 <span class="crayon-v">
                  span
                 </span>
                 <span class="crayon-o">
                  &gt;
                 </span>
                 <span class="crayon-o">
                  &lt;
                 </span>
                 <span class="crayon-e">
                  span
                 </span>
                 <span class="crayon-t">
                  class
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-s">
                  "hljs-preprocessor"
                 </span>
                 <span class="crayon-o">
                  &gt;
                 </span>
                 <span class="crayon-sy">
                  .
                 </span>
                 <span class="crayon-v">
                  SGD
                 </span>
                 <span class="crayon-o">
                  &lt;
                 </span>
                 <span class="crayon-o">
                  /
                 </span>
                 <span class="crayon-v">
                  span
                 </span>
                 <span class="crayon-o">
                  &gt;
                 </span>
                 <span class="crayon-sy">
                  (
                 </span>
                 <span class="crayon-v">
                  lr
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-o">
                  &lt;
                 </span>
                 <span class="crayon-e">
                  span
                 </span>
                 <span class="crayon-t">
                  class
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-s">
                  "hljs-number"
                 </span>
                 <span class="crayon-o">
                  &gt;
                 </span>
                 <span class="crayon-cn">
                  0.01
                 </span>
                 <span class="crayon-o">
                  &lt;
                 </span>
                 <span class="crayon-o">
                  /
                 </span>
                 <span class="crayon-v">
                  span
                 </span>
                 <span class="crayon-o">
                  &gt;
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  momentum
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-o">
                  &lt;
                 </span>
                 <span class="crayon-e">
                  span
                 </span>
                 <span class="crayon-t">
                  class
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-s">
                  "hljs-number"
                 </span>
                 <span class="crayon-o">
                  &gt;
                 </span>
                 <span class="crayon-cn">
                  0.9
                 </span>
                 <span class="crayon-o">
                  &lt;
                 </span>
                 <span class="crayon-o">
                  /
                 </span>
                 <span class="crayon-v">
                  span
                 </span>
                 <span class="crayon-o">
                  &gt;
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  decay
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-o">
                  &lt;
                 </span>
                 <span class="crayon-e">
                  span
                 </span>
                 <span class="crayon-t">
                  class
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-s">
                  "hljs-number"
                 </span>
                 <span class="crayon-o">
                  &gt;
                 </span>
                 <span class="crayon-cn">
                  0.9
                 </span>
                 <span class="crayon-o">
                  &lt;
                 </span>
                 <span class="crayon-o">
                  /
                 </span>
                 <span class="crayon-v">
                  span
                 </span>
                 <span class="crayon-o">
                  &gt;
                 </span>
                 <span class="crayon-sy">
                  ,
                 </span>
                 <span class="crayon-h">
                 </span>
                 <span class="crayon-v">
                  nesterov
                 </span>
                 <span class="crayon-o">
                  =
                 </span>
                 <span class="crayon-t">
                  False
                 </span>
                 <span class="crayon-sy">
                  )
                 </span>
                </div>
               </div>
              </td>
             </tr>
            </tbody>
           </table>
          </div>
         </div>
         <!-- [Format Time: 0.0028 seconds] -->
         <p>
         </p>
         <ul class="pre-numbering">
          <li>
           1
          </li>
         </ul>
         <p>
          上面的代码是SGD的使用方法，lr表示学习速率,momentum表示动量项，decay是学习速率的衰减系数(每个epoch衰减一次),Nesterov的值是False或者True，表示使不使用Nesterov momentum。其他的请参考文档。
         </p>
        </li>
        <li>
         <strong>
          Objectives
         </strong>
         这是目标函数模块，keras提供了mean_squared_error，mean_absolute_error ，squared_hinge，hinge，binary_crossentropy，categorical_crossentropy这几种目标函数。
         <p>
          这里binary_crossentropy 和 categorical_crossentropy也就是logloss
         </p>
        </li>
        <li>
         <strong>
          Activations
         </strong>
         这是激活函数模块，keras提供了linear、sigmoid、hard_sigmoid、tanh、softplus、relu、softplus，另外softmax也放在Activations模块里(我觉得放在layers模块里更合理些）。此外，像LeakyReLU和PReLU这种比较新的激活函数，keras在keras.layers.advanced_activations模块里提供。
        </li>
        <li>
         <strong>
          Initializations
         </strong>
         这是参数初始化模块，在添加layer的时候调用init进行初始化。keras提供了uniform、lecun_uniform、normal、orthogonal、zero、glorot_normal、he_normal这几种。
        </li>
        <li>
         <strong>
          layers
         </strong>
         layers模块包含了core、convolutional、recurrent、advanced_activations、normalization、embeddings这几种layer。
         <p>
          其中core里面包含了flatten(CNN的全连接层之前需要把二维特征图flatten成为一维的)、reshape（CNN输入时将一维的向量弄成二维的）、dense(就是隐藏层，dense是稠密的意思),还有其他的就不介绍了。convolutional层基本就是Theano的Convolution2D的封装。
         </p>
        </li>
        <li>
         <strong>
          Preprocessing
         </strong>
         这是预处理模块，包括序列数据的处理，文本数据的处理，图像数据的处理。重点看一下图像数据的处理，keras提供了ImageDataGenerator函数,实现data augmentation，数据集扩增，对图像做一些弹性变换，比如水平翻转，垂直翻转，旋转等。
        </li>
        <li>
         <strong>
          Models
         </strong>
         这是最主要的模块，模型。上面定义了各种基本组件，model是将它们组合起来，下面通过一个实例来说明。
        </li>
       </ul>
       <h2 id="3一个实例用cnn分类mnist">
        <p name="t2">
        </p>
        3.一个实例：用CNN分类Mnist
       </h2>
       <ul>
        <li>
         <strong>
          数据下载
         </strong>
         Mnist数据在其官网上有提供，但是不是图像格式的，因为我们通常都是直接处理图像，为了以后程序能复用，我把它弄成图像格式的，这里可以下载：
         <a href="http://pan.baidu.com/s/1qCdS6">
          http://pan.baidu.com/s/1qCdS6
         </a>
         ，共有42000张图片。
        </li>
        <li>
         <strong>
          读取图片数据
         </strong>
         keras要求输入的数据格式是numpy.array类型（numpy是一个python的数值计算的库），所以需要写一个脚本来读入mnist图像，保存为一个四维的data，还有一个一维的label，代码：
        </li>
       </ul>
       <p>
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57685c9c6b265139958726" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          &lt;span class="hljs-comment"&gt;#coding:utf-8&lt;/span&gt;
&lt;span class="hljs-string"&gt;"""
Author:wepon
Source:https://github.com/wepe
file:data.py
"""&lt;/span&gt;

&lt;span class="hljs-keyword"&gt;import&lt;/span&gt; os
&lt;span class="hljs-keyword"&gt;from&lt;/span&gt; PIL &lt;span class="hljs-keyword"&gt;import&lt;/span&gt; Image
&lt;span class="hljs-keyword"&gt;import&lt;/span&gt; numpy &lt;span class="hljs-keyword"&gt;as&lt;/span&gt; np

&lt;span class="hljs-comment"&gt;#读取文件夹mnist下的42000张图片，图片为灰度图，所以为1通道，&lt;/span&gt;
&lt;span class="hljs-comment"&gt;#如果是将彩色图作为输入,则将1替换为3,图像大小28*28&lt;/span&gt;
&lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;def&lt;/span&gt; &lt;span class="hljs-title"&gt;load_data&lt;/span&gt;&lt;span class="hljs-params"&gt;()&lt;/span&gt;:&lt;/span&gt;
    data = np.empty((&lt;span class="hljs-number"&gt;42000&lt;/span&gt;,&lt;span class="hljs-number"&gt;1&lt;/span&gt;,&lt;span class="hljs-number"&gt;28&lt;/span&gt;,&lt;span class="hljs-number"&gt;28&lt;/span&gt;),dtype=&lt;span class="hljs-string"&gt;"float32"&lt;/span&gt;)
    label = np.empty((&lt;span class="hljs-number"&gt;42000&lt;/span&gt;,),dtype=&lt;span class="hljs-string"&gt;"uint8"&lt;/span&gt;)

    imgs = os.listdir(&lt;span class="hljs-string"&gt;"./mnist"&lt;/span&gt;)
    num = len(imgs)
    &lt;span class="hljs-keyword"&gt;for&lt;/span&gt; i &lt;span class="hljs-keyword"&gt;in&lt;/span&gt; range(num):
        img = Image.open(&lt;span class="hljs-string"&gt;"./mnist/"&lt;/span&gt;+imgs[i])
        arr = np.asarray(img,dtype=&lt;span class="hljs-string"&gt;"float32"&lt;/span&gt;)
        data[i,:,:,:] = arr
        label[i] = int(imgs[i].split(&lt;span class="hljs-string"&gt;'.'&lt;/span&gt;)[&lt;span class="hljs-number"&gt;0&lt;/span&gt;])
    &lt;span class="hljs-keyword"&gt;return&lt;/span&gt; data,label
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b265139958726-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b265139958726-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b265139958726-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b265139958726-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b265139958726-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b265139958726-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b265139958726-14">
               14
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-15">
               15
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b265139958726-16">
               16
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-17">
               17
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b265139958726-18">
               18
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-19">
               19
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b265139958726-20">
               20
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-21">
               21
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b265139958726-22">
               22
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-23">
               23
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b265139958726-24">
               24
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b265139958726-25">
               25
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-1">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-comment"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #coding:utf-8&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b265139958726-2">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                ""
               </span>
               <span class="crayon-s">
                "
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-3">
               <span class="crayon-s">
                Author:wepon
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b265139958726-4">
               <span class="crayon-s">
                Source:https://github.com/wepe
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-5">
               <span class="crayon-s">
                file:data.py
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b265139958726-6">
               <span class="crayon-s">
                "
               </span>
               <span class="crayon-s">
                ""
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-7">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b265139958726-8">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-keyword"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-v">
                import
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                os
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-9">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-keyword"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-v">
                from
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                PIL
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-keyword"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-v">
                import
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                Image
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b265139958726-10">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-keyword"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-v">
                import
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                numpy
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-keyword"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-st">
                as
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                np
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-11">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b265139958726-12">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-comment"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #读取文件夹mnist下的42000张图片，图片为灰度图，所以为1通道，&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-13">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-comment"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #如果是将彩色图作为输入,则将1替换为3,图像大小28*28&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b265139958726-14">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-function"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-keyword"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-v">
                def
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-title"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-v">
                load_data
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-params"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-15">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                data
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                np
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                empty
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                42000
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                28
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                28
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                dtype
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                "float32"
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b265139958726-16">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                label
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                np
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                empty
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                42000
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                dtype
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                "uint8"
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-17">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b265139958726-18">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                imgs
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                os
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                listdir
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                "./mnist"
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-19">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                num
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                len
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                imgs
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b265139958726-20">
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-keyword"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                i
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-keyword"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                range
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                num
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-21">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                img
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                Image
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                open
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                "./mnist/"
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-o">
                +
               </span>
               <span class="crayon-v">
                imgs
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                i
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b265139958726-22">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                arr
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                np
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                asarray
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                img
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                dtype
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                "float32"
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-23">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                data
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                i
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                arr
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b265139958726-24">
               <span class="crayon-e">
               </span>
               <span class="crayon-v">
                label
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                i
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                int
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                imgs
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                i
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                split
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                '.'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b265139958726-25">
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-keyword"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                data
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                label
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0342 seconds] -->
       <p>
       </p>
       <ul class="pre-numbering">
        <li>
         构建CNN，训练短短二十多行代码，构建一个三个卷积层的CNN，直接读下面的代码吧，有注释，很容易读懂：
        </li>
       </ul>
       <p>
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57685c9c6b26e096570660" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          &lt;span class="hljs-preprocessor"&gt;#导入各种用到的模块组件&lt;/span&gt;
from __future__ import absolute_import
from __future__ import print_function
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.advanced_activations import PReLU
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD, Adadelta, Adagrad
from keras.utils import np_utils, generic_utils
from six.moves import range
from data import load_data

&lt;span class="hljs-preprocessor"&gt;#加载数据&lt;/span&gt;
data, label = load_data()
print(data.shape[&lt;span class="hljs-number"&gt;0&lt;/span&gt;], &lt;span class="hljs-string"&gt;' samples'&lt;/span&gt;)

&lt;span class="hljs-preprocessor"&gt;#label为0~9共10个类别，keras要求格式为binary class matrices,转化一下，直接调用keras提供的这个函数&lt;/span&gt;
label = np_utils.to_categorical(label, &lt;span class="hljs-number"&gt;10&lt;/span&gt;)

&lt;span class="hljs-preprocessor"&gt;###############&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#开始建立CNN模型&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;###############&lt;/span&gt;

&lt;span class="hljs-preprocessor"&gt;#生成一个model&lt;/span&gt;
model = Sequential()

&lt;span class="hljs-preprocessor"&gt;#第一个卷积层，4个卷积核，每个卷积核大小5*5。1表示输入的图片的通道,灰度图为1通道。&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#border_mode可以是valid或者full，具体看这里说明：http://deeplearning.net/software/theano/library/tensor/nnet/conv.html#theano.tensor.nnet.conv.conv2d&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#激活函数用tanh&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#你还可以在model.add(Activation('tanh'))后加上dropout的技巧: model.add(Dropout(0.5))&lt;/span&gt;
model.add(Convolution2D(&lt;span class="hljs-number"&gt;4&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-number"&gt;5&lt;/span&gt;, &lt;span class="hljs-number"&gt;5&lt;/span&gt;, border_mode=&lt;span class="hljs-string"&gt;'valid'&lt;/span&gt;)) 
model.add(Activation(&lt;span class="hljs-string"&gt;'tanh'&lt;/span&gt;))

&lt;span class="hljs-preprocessor"&gt;#第二个卷积层，8个卷积核，每个卷积核大小3*3。4表示输入的特征图个数，等于上一层的卷积核个数&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#激活函数用tanh&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#采用maxpooling，poolsize为(2,2)&lt;/span&gt;
model.add(Convolution2D(&lt;span class="hljs-number"&gt;8&lt;/span&gt;,&lt;span class="hljs-number"&gt;4&lt;/span&gt;, &lt;span class="hljs-number"&gt;3&lt;/span&gt;, &lt;span class="hljs-number"&gt;3&lt;/span&gt;, border_mode=&lt;span class="hljs-string"&gt;'valid'&lt;/span&gt;))
model.add(Activation(&lt;span class="hljs-string"&gt;'tanh'&lt;/span&gt;))
model.add(MaxPooling2D(poolsize=(&lt;span class="hljs-number"&gt;2&lt;/span&gt;, &lt;span class="hljs-number"&gt;2&lt;/span&gt;)))

&lt;span class="hljs-preprocessor"&gt;#第三个卷积层，16个卷积核，每个卷积核大小3*3&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#激活函数用tanh&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#采用maxpooling，poolsize为(2,2)&lt;/span&gt;
model.add(Convolution2D(&lt;span class="hljs-number"&gt;16&lt;/span&gt;, &lt;span class="hljs-number"&gt;8&lt;/span&gt;, &lt;span class="hljs-number"&gt;3&lt;/span&gt;, &lt;span class="hljs-number"&gt;3&lt;/span&gt;, border_mode=&lt;span class="hljs-string"&gt;'valid'&lt;/span&gt;)) 
model.add(Activation(&lt;span class="hljs-string"&gt;'tanh'&lt;/span&gt;))
model.add(MaxPooling2D(poolsize=(&lt;span class="hljs-number"&gt;2&lt;/span&gt;, &lt;span class="hljs-number"&gt;2&lt;/span&gt;)))

&lt;span class="hljs-preprocessor"&gt;#全连接层，先将前一层输出的二维特征图flatten为一维的。&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#Dense就是隐藏层。16就是上一层输出的特征图个数。4是根据每个卷积层计算出来的：(28-5+1)得到24,(24-3+1)/2得到11，(11-3+1)/2得到4&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#全连接有128个神经元节点,初始化方式为normal&lt;/span&gt;
model.add(Flatten())
model.add(Dense(&lt;span class="hljs-number"&gt;16&lt;/span&gt;*&lt;span class="hljs-number"&gt;4&lt;/span&gt;*&lt;span class="hljs-number"&gt;4&lt;/span&gt;, &lt;span class="hljs-number"&gt;128&lt;/span&gt;, init=&lt;span class="hljs-string"&gt;'normal'&lt;/span&gt;))
model.add(Activation(&lt;span class="hljs-string"&gt;'tanh'&lt;/span&gt;))

&lt;span class="hljs-preprocessor"&gt;#Softmax分类，输出是10类别&lt;/span&gt;
model.add(Dense(&lt;span class="hljs-number"&gt;128&lt;/span&gt;, &lt;span class="hljs-number"&gt;10&lt;/span&gt;, init=&lt;span class="hljs-string"&gt;'normal'&lt;/span&gt;))
model.add(Activation(&lt;span class="hljs-string"&gt;'softmax'&lt;/span&gt;))

&lt;span class="hljs-preprocessor"&gt;#############&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#开始训练模型&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;##############&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#使用SGD + momentum&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#model.compile里的参数loss就是损失函数(目标函数)&lt;/span&gt;
sgd = SGD(l2=&lt;span class="hljs-number"&gt;0.0&lt;/span&gt;,lr=&lt;span class="hljs-number"&gt;0.05&lt;/span&gt;, decay=&lt;span class="hljs-number"&gt;1e-6&lt;/span&gt;, momentum=&lt;span class="hljs-number"&gt;0.9&lt;/span&gt;, nesterov=True)
model.compile(loss=&lt;span class="hljs-string"&gt;'categorical_crossentropy'&lt;/span&gt;, optimizer=sgd,class_mode=&lt;span class="hljs-string"&gt;"categorical"&lt;/span&gt;)

&lt;span class="hljs-preprocessor"&gt;#调用fit方法，就是一个训练过程. 训练的epoch数设为10，batch_size为100．&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#数据经过随机打乱shuffle=True。verbose=1，训练过程中输出的信息，0、1、2三种方式都可以，无关紧要。show_accuracy=True，训练时每一个epoch都输出accuracy。&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#validation_split=0.2，将20%的数据作为验证集。&lt;/span&gt;
model.fit(data, label, batch_size=&lt;span class="hljs-number"&gt;100&lt;/span&gt;,nb_epoch=&lt;span class="hljs-number"&gt;10&lt;/span&gt;,shuffle=True,verbose=&lt;span class="hljs-number"&gt;1&lt;/span&gt;,show_accuracy=True,validation_split=&lt;span class="hljs-number"&gt;0.2&lt;/span&gt;)

&lt;span class="hljs-preprocessor"&gt;#fit方法在达到设定的nb_epoch时结束，并且自动地保存了效果最好的model,之后你可以调用model.evaluate()方法对测试数据进行测试，&lt;/span&gt;
&lt;span class="hljs-preprocessor"&gt;#还有model.predict_classes,model.predict_proba等方法,具体请看文档。&lt;/span&gt;
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-14">
               14
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-15">
               15
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-16">
               16
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-17">
               17
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-18">
               18
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-19">
               19
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-20">
               20
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-21">
               21
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-22">
               22
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-23">
               23
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-24">
               24
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-25">
               25
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-26">
               26
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-27">
               27
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-28">
               28
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-29">
               29
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-30">
               30
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-31">
               31
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-32">
               32
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-33">
               33
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-34">
               34
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-35">
               35
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-36">
               36
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-37">
               37
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-38">
               38
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-39">
               39
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-40">
               40
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-41">
               41
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-42">
               42
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-43">
               43
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-44">
               44
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-45">
               45
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-46">
               46
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-47">
               47
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-48">
               48
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-49">
               49
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-50">
               50
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-51">
               51
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-52">
               52
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-53">
               53
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-54">
               54
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-55">
               55
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-56">
               56
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-57">
               57
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-58">
               58
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-59">
               59
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-60">
               60
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-61">
               61
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-62">
               62
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-63">
               63
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-64">
               64
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-65">
               65
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-66">
               66
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-67">
               67
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-68">
               68
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-69">
               69
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-70">
               70
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-71">
               71
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-72">
               72
              </div>
              <div class="crayon-num" data-line="crayon-57685c9c6b26e096570660-73">
               73
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57685c9c6b26e096570660-74">
               74
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-1">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #导入各种用到的模块组件&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-2">
               <span class="crayon-e">
                from
               </span>
               <span class="crayon-e">
                __future__
               </span>
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-e">
                absolute_import
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-3">
               <span class="crayon-e">
                from
               </span>
               <span class="crayon-e">
                __future__
               </span>
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-e">
                print_function
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-4">
               <span class="crayon-e">
                from
               </span>
               <span class="crayon-v">
                keras
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                preprocessing
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                image
               </span>
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-e">
                ImageDataGenerator
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-5">
               <span class="crayon-e">
                from
               </span>
               <span class="crayon-v">
                keras
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                models
               </span>
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-e">
                Sequential
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-6">
               <span class="crayon-e">
                from
               </span>
               <span class="crayon-v">
                keras
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                layers
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                core
               </span>
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-v">
                Dense
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                Dropout
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                Activation
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Flatten
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-7">
               <span class="crayon-e">
                from
               </span>
               <span class="crayon-v">
                keras
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                layers
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                advanced_activations
               </span>
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-e">
                PReLU
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-8">
               <span class="crayon-e">
                from
               </span>
               <span class="crayon-v">
                keras
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                layers
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                convolutional
               </span>
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-v">
                Convolution2D
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                MaxPooling2D
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-9">
               <span class="crayon-e">
                from
               </span>
               <span class="crayon-v">
                keras
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                optimizers
               </span>
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-v">
                SGD
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                Adadelta
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Adagrad
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-10">
               <span class="crayon-e">
                from
               </span>
               <span class="crayon-v">
                keras
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                utils
               </span>
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-v">
                np_utils
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                generic_utils
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-11">
               <span class="crayon-e">
                from
               </span>
               <span class="crayon-v">
                six
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                moves
               </span>
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-e">
                range
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-12">
               <span class="crayon-e">
                from
               </span>
               <span class="crayon-e">
                data
               </span>
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-v">
                load_data
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-13">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-14">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #加载数据&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-15">
               <span class="crayon-v">
                data
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                label
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                load_data
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-16">
               <span class="crayon-e">
                print
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                data
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                shape
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                ' samples'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-17">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-18">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #label为0~9共10个类别，keras要求格式为binary class matrices,转化一下，直接调用keras提供的这个函数&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-19">
               <span class="crayon-v">
                label
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                np_utils
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                to_categorical
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                label
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                10
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-20">
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-21">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                ###############&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-22">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #开始建立CNN模型&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-23">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                ###############&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-24">
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-25">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #生成一个model&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-26">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Sequential
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-27">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-28">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #第一个卷积层，4个卷积核，每个卷积核大小5*5。1表示输入的图片的通道,灰度图为1通道。&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-29">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #border_mode可以是valid或者full，具体看这里说明：http://deeplearning.net/software/theano/library/tensor/nnet/conv.html#theano.tensor.nnet.conv.conv2d&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-30">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #激活函数用tanh&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-31">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #你还可以在model.add(Activation('tanh'))后加上dropout的技巧: model.add(Dropout(0.5))&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-32">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                Convolution2D
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                4
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                5
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                5
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                border_mode
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                'valid'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-h">
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-33">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                Activation
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                'tanh'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-34">
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-35">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #第二个卷积层，8个卷积核，每个卷积核大小3*3。4表示输入的特征图个数，等于上一层的卷积核个数&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-36">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #激活函数用tanh&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-37">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #采用maxpooling，poolsize为(2,2)&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-38">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                Convolution2D
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                8
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                4
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                3
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                3
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                border_mode
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                'valid'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-39">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                Activation
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                'tanh'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-40">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                MaxPooling2D
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                poolsize
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                2
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                2
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-41">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-42">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #第三个卷积层，16个卷积核，每个卷积核大小3*3&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-43">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #激活函数用tanh&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-44">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #采用maxpooling，poolsize为(2,2)&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-45">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                Convolution2D
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                16
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                8
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                3
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                3
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                border_mode
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                'valid'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-h">
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-46">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                Activation
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                'tanh'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-47">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                MaxPooling2D
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                poolsize
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                2
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                2
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-48">
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-49">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #全连接层，先将前一层输出的二维特征图flatten为一维的。&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-50">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #Dense就是隐藏层。16就是上一层输出的特征图个数。4是根据每个卷积层计算出来的：(28-5+1)得到24,(24-3+1)/2得到11，(11-3+1)/2得到4&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-51">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #全连接有128个神经元节点,初始化方式为normal&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-52">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                Flatten
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-53">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                Dense
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                16
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                4
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                4
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                128
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                init
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                'normal'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-54">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                Activation
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                'tanh'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-55">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-56">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #Softmax分类，输出是10类别&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-57">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                Dense
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                128
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                10
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                init
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                'normal'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-58">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                Activation
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                'softmax'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-59">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-60">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #############&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-61">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #开始训练模型&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-62">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                ##############&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-63">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #使用SGD + momentum&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-64">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #model.compile里的参数loss就是损失函数(目标函数)&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-65">
               <span class="crayon-v">
                sgd
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                SGD
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                l2
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                0.0
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                lr
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                0.05
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                decay
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                1e
               </span>
               <span class="crayon-o">
                -
               </span>
               <span class="crayon-cn">
                6
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                momentum
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                0.9
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                nesterov
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-66">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                compile
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                loss
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                'categorical_crossentropy'
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                optimizer
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-v">
                sgd
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                class_mode
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-string"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-s">
                "categorical"
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-67">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-68">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #调用fit方法，就是一个训练过程. 训练的epoch数设为10，batch_size为100．&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-69">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #数据经过随机打乱shuffle=True。verbose=1，训练过程中输出的信息，0、1、2三种方式都可以，无关紧要。show_accuracy=True，训练时每一个epoch都输出accuracy。&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-70">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #validation_split=0.2，将20%的数据作为验证集。&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-71">
               <span class="crayon-v">
                model
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                fit
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                data
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                label
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                batch_size
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                100
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                nb_epoch
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                10
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                shuffle
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                verbose
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                show_accuracy
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                validation_split
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-number"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-cn">
                0.2
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-72">
              </div>
              <div class="crayon-line" id="crayon-57685c9c6b26e096570660-73">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #fit方法在达到设定的nb_epoch时结束，并且自动地保存了效果最好的model,之后你可以调用model.evaluate()方法对测试数据进行测试，&lt;/span&gt;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57685c9c6b26e096570660-74">
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-p">
                #还有model.predict_classes,model.predict_proba等方法,具体请看文档。&lt;/span&gt;
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0767 seconds] -->
       <p>
       </p>
       <ul>
        <li>
         <strong>
          代码使用与结果
         </strong>
        </li>
       </ul>
       <p>
        代码放在我github的机器学习仓库里：
        <a href="https://github.com/wepe/MachineLearning">
         https://github.com/wepe/MachineLearning
        </a>
        ,非github用户直接点右下的DownloadZip。
       </p>
       <p>
        在/DeepLearning Tutorials/keras_usage目录下包括
        <code>
         data.py
        </code>
        ,
        <code>
         cnn.py
        </code>
        两份代码，下载Mnist数据后解压到该目录下，运行
        <code>
         cnn.py
        </code>
        这份文件即可。
       </p>
       <p>
        结果如下所示，在Epoch 9达到了0.98的训练集识别率和0.97的验证集识别率：
       </p>
       <p>
        <a href="http://dataunion.org/wp-content/uploads/2015/05/20150430170244640.png">
         <img src="http://dataunion.org/wp-content/uploads/2015/05/20150430170244640.png"/>
        </a>
       </p>
       <hr/>
       <p>
        <a href="http://blog.csdn.net/u012162613/article/details/45397033">
         以上
        </a>
        总结了Keras的基本使用方法，相信用过的同学都会觉得不可思议，太简洁了。十多天前，我在github上发现这个框架的时候，关注Keras的人还比较少，这两天无论是github还是微薄，都看到越来越多的人关注和使用Keras。所以这篇文章就简单地再介绍一下Keras的使用,方便各位入门。
       </p>
       <p>
        主要包括以下三个内容：
       </p>
       <ul>
        <li>
         训练CNN并保存训练好的模型。
        </li>
        <li>
         将CNN用于特征提取，用提取出来的特征训练SVM。
        </li>
        <li>
         可视化CNN卷积层后的特征图。
        </li>
       </ul>
       <p>
        仍然以Mnist为例，代码中用的Mnist数据到这里下载
        <br/>
        <a href="http://pan.baidu.com/s/1qCdS6">
         http://pan.baidu.com/s/1qCdS6
        </a>
        ,本文的代码在我的github上:
        <a href="https://github.com/wepe/MachineLearning/tree/master/DeepLearning%20Tutorials">
         dive_into _keras
        </a>
       </p>
       <hr/>
       <h2 id="1-加载数据">
        <p name="t0">
        </p>
        1. 加载数据
       </h2>
       <p>
        数据是图片格式，利用pyhton的PIL模块读取，并转为numpy.array类型。这部分的代码在
        <code>
         data.py
        </code>
        里：
       </p>
       <hr/>
       <h2 id="2-训练cnn并保存训练好的cnn模型">
        <p name="t1">
        </p>
        2. 训练CNN并保存训练好的CNN模型
       </h2>
       <p>
        将上一步加载进来的数据分为训练数据（X_train，30000个样本）和验证数据（X_val，12000个样本），构建CNN模型并训练。训练过程中，每一个epoch得到的val-accuracy都不一样，我们保存达到最好的val-accuracy时的模型，利用Python的cPickle模块保持。（Keras的开发者最近在添加用hdf5保持模型的功能，我试了一下，没用成功，去github发了issue也没人回，估计还没完善，hdf5压缩率会更高，保存下来的文件会更小。）
       </p>
       <p>
        这部分的代码在
        <code>
         cnn.py
        </code>
        里，运行:
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57685c9c6b27d702769744" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          python cnn&lt;span class="hljs-preprocessor"&gt;.py&lt;/span&gt;
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57685c9c6b27d702769744-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57685c9c6b27d702769744-1">
               <span class="crayon-e">
                python
               </span>
               <span class="crayon-v">
                cnn
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                py
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0009 seconds] -->
       <p>
       </p>
       <ul class="pre-numbering">
        <li>
         1
        </li>
       </ul>
       <p>
        在第Epoch 4得到96.45%的validation accuracy,运行完后会得到model.pkl这份文件，保存的就是96.45%对应的模型：
       </p>
       <p>
        <a href="http://dataunion.org/wp-content/uploads/2015/05/20150508155724085.png">
         <img src="http://dataunion.org/wp-content/uploads/2015/05/20150508155724085.png"/>
        </a>
       </p>
       <hr/>
       <h2 id="3将cnn用于特征提取用提取出来的特征训练svm">
        <p name="t2">
        </p>
        3.将CNN用于特征提取，用提取出来的特征训练SVM
       </h2>
       <p>
        上一步得到了一个val-accuracy为96.45%的CNN模型，在一些论文中经常会看到用CNN的全连接层的输出作为特征，然后去训练其他分类器。这里我也试了一下，用全连接层的输出作为样本的特征向量，训练SVM。SVM用的是scikit learn里的算法。
       </p>
       <p>
        这部分代码在
        <code>
         cnn-svm.py
        </code>
        ,运行：
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57685c9c6b285786391357" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          python cnn-svm&lt;span class="hljs-preprocessor"&gt;.py&lt;/span&gt;
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57685c9c6b285786391357-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57685c9c6b285786391357-1">
               <span class="crayon-e">
                python
               </span>
               <span class="crayon-v">
                cnn
               </span>
               <span class="crayon-o">
                -
               </span>
               <span class="crayon-v">
                svm
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                py
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0014 seconds] -->
       <p>
       </p>
       <ul class="pre-numbering">
        <li>
         1
        </li>
       </ul>
       <p>
        得到下图的输出，可以看到，cnn-svm的准确率提高到97.89%：
       </p>
       <p>
        <a href="http://dataunion.org/wp-content/uploads/2015/05/20150508155806689.png">
         <img src="http://dataunion.org/wp-content/uploads/2015/05/20150508155806689.png"/>
        </a>
       </p>
       <hr/>
       <h2 id="4可视化cnn卷积层后的特征图">
        <p name="t3">
        </p>
        4.可视化CNN卷积层后的特征图
       </h2>
       <p>
        将卷积层和全连接层后的特征图、特征向量以图片形式展示出来，用到matplotlib这个库。这部分代码在
        <code>
         get_feature_map.py
        </code>
        里。运行：
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57685c9c6b28c553852827" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          python get_feature_map&lt;span class="hljs-preprocessor"&gt;.py&lt;/span&gt;
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57685c9c6b28c553852827-1">
               1
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57685c9c6b28c553852827-1">
               <span class="crayon-e">
                python
               </span>
               <span class="crayon-v">
                get_feature_map
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-e">
                span
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-s">
                "hljs-preprocessor"
               </span>
               <span class="crayon-o">
                &gt;
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                py
               </span>
               <span class="crayon-o">
                &lt;
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                span
               </span>
               <span class="crayon-o">
                &gt;
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.0009 seconds] -->
       <p>
       </p>
       <ul class="pre-numbering">
        <li>
         1
        </li>
       </ul>
       <p>
        得到全连接层的输出，以及第一个卷积层输出的4个特征图：
       </p>
       <p>
        <a href="http://dataunion.org/wp-content/uploads/2015/05/20150508155842678.png">
         <img src="http://dataunion.org/wp-content/uploads/2015/05/20150508155842678.png"/>
        </a>
       </p>
       <p>
        <a href="http://dataunion.org/wp-content/uploads/2015/05/20150508155724909.png">
         <img src="http://dataunion.org/wp-content/uploads/2015/05/20150508155724909.png"/>
        </a>
       </p>
       <p>
        <a href="http://dataunion.org/wp-content/uploads/2015/05/20150508155810914.png">
         <img src="http://dataunion.org/wp-content/uploads/2015/05/20150508155810914.png"/>
        </a>
       </p>
       <p>
        <a href="http://dataunion.org/wp-content/uploads/2015/05/20150508160043578.png">
         <img src="http://dataunion.org/wp-content/uploads/2015/05/20150508160043578.png"/>
        </a>
       </p>
       <p>
       </p>
       <blockquote>
        <p>
         作者：
         <a class="user_name" href="http://my.csdn.net/u012162613" target="_blank">
          wphh
         </a>
        </p>
        <p>
         文章出处：
         <a href="http://blog.csdn.net/u012162613/article/details/45397033">
          http://blog.csdn.net/u012162613/article/details/45397033
         </a>
        </p>
       </blockquote>
      </div>
      <div>
       <strong>
        注：转载文章均来自于公开网络，仅供学习使用，不会用于任何商业用途，如果侵犯到原作者的权益，请您与我们联系删除或者授权事宜，联系邮箱：contact@dataunion.org。转载数盟网站文章请注明原文章作者，否则产生的任何版权纠纷与数盟无关。
       </strong>
      </div>
      <!--content_text-->
      <div class="fenxian">
       <!-- JiaThis Button BEGIN -->
       <div class="jiathis_style_32x32">
        <p class="jiathis_button_weixin">
        </p>
        <p class="jiathis_button_tsina">
        </p>
        <p class="jiathis_button_qzone">
        </p>
        <p class="jiathis_button_cqq">
        </p>
        <p class="jiathis_button_tumblr">
        </p>
        <a class="jiathis jiathis_txt jtico jtico_jiathis" href="http://www.jiathis.com/share" target="_blank">
        </a>
        <p class="jiathis_counter_style">
        </p>
       </div>
       <!-- JiaThis Button END -->
      </div>
     </article>
     <!--content-->
     <!--相关文章-->
     <div class="xianguan">
      <div class="xianguantitle">
       相关文章！
      </div>
      <ul class="pic">
       <li>
        <a href="http://dataunion.org/24220.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/05/100910rrxp5plglxk4lcle-291x200.png"/>
        </a>
        <a class="link" href="http://dataunion.org/24220.html" rel="bookmark" title="520大数据：八成男性期待被表白 “套路”也是爱">
         520大数据：八成男性期待被表白 “套路”也是爱
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/23785.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/04/u7145986293872240263fm21gp0-298x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/23785.html" rel="bookmark" title="Docker安全基准：22项新增要点概述">
         Docker安全基准：22项新增要点概述
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/23703.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/04/1317020-300x163.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/23703.html" rel="bookmark" title="推荐：五个不容错过的数据科学家博客">
         推荐：五个不容错过的数据科学家博客
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/23507.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/03/110606wjpugdldig4idddn.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/23507.html" rel="bookmark" title="解密深度学习在智能推荐系统的实践与应用">
         解密深度学习在智能推荐系统的实践与应用
        </a>
       </li>
      </ul>
     </div>
     <!--相关文章-->
     <div class="comment" id="comments">
      <!-- You can start editing here. -->
      <!-- If comments are open, but there are no comments. -->
      <div class="title">
       期待你一针见血的评论，Come on！
      </div>
      <div id="respond">
       <p>
        不用想啦，马上
        <a href="http://dataunion.org/wp-login.php?redirect_to=http%3A%2F%2Fdataunion.org%2F16787.html">
         "登录"
        </a>
        发表自已的想法.
       </p>
      </div>
     </div>
     <!-- .nav-single -->
    </div>
    <!--Container End-->
    <aside id="sitebar">
     <div class="sitebar_list2">
      <div class="wptag">
       <span class="tagtitle">
        热门标签+
       </span>
       <div class="tagg">
        <ul class="menu" id="menu-%e5%8f%8b%e6%83%85%e9%93%be%e6%8e%a5">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1605" id="menu-item-1605">
          <a href="http://taidizh.com/">
           泰迪智慧
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20884" id="menu-item-20884">
          <a href="http://www.transwarp.cn/">
           星环科技
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-3538" id="menu-item-3538">
          <a href="http://datall.org/">
           珈和遥感
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20888" id="menu-item-20888">
          <a href="http://www.chinahadoop.cn/">
           小象学院
          </a>
         </li>
        </ul>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <div class="textwidget">
       <div align="center">
        <a href="http://study.163.com/course/courseMain.htm?courseId=991022" target="_blank">
         <img src="http://dataunion.org/wp-content/uploads/2016/03/dv.jpg"/>
        </a>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       文章分类
      </h4>
      <div class="tagcloud">
       <a class="tag-link-44" href="http://dataunion.org/category/industry/demo" style="font-size: 10.204724409449pt;" title="4个话题">
        Demo展示
       </a>
       <a class="tag-link-31" href="http://dataunion.org/category/experts" style="font-size: 15.826771653543pt;" title="52个话题">
        专家团队
       </a>
       <a class="tag-link-870" href="http://dataunion.org/category/tech/ai" style="font-size: 19.795275590551pt;" title="273个话题">
        人工智能
       </a>
       <a class="tag-link-488" href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f" style="font-size: 8pt;" title="1个话题">
        加入数盟
       </a>
       <a class="tag-link-869" href="http://dataunion.org/category/tech/viz" style="font-size: 17.204724409449pt;" title="93个话题">
        可视化
       </a>
       <a class="tag-link-30" href="http://dataunion.org/category/partners" style="font-size: 10.645669291339pt;" title="5个话题">
        合作伙伴
       </a>
       <a class="tag-link-889" href="http://dataunion.org/category/parterc" style="font-size: 11.582677165354pt;" title="8个话题">
        合作会议
       </a>
       <a class="tag-link-104" href="http://dataunion.org/category/books" style="font-size: 12.96062992126pt;" title="15个话题">
        图书
       </a>
       <a class="tag-link-220" href="http://dataunion.org/category/tech/base" style="font-size: 19.850393700787pt;" title="281个话题">
        基础架构
       </a>
       <a class="tag-link-219" href="http://dataunion.org/category/tech/analysis" style="font-size: 19.409448818898pt;" title="232个话题">
        数据分析
       </a>
       <a class="tag-link-887" href="http://dataunion.org/category/tech/dm" style="font-size: 13.291338582677pt;" title="17个话题">
        数据挖掘
       </a>
       <a class="tag-link-34" href="http://dataunion.org/category/tech" style="font-size: 20.732283464567pt;" title="404个话题">
        文章
       </a>
       <a class="tag-link-1" href="http://dataunion.org/category/uncategorized" style="font-size: 22pt;" title="693个话题">
        未分类
       </a>
       <a class="tag-link-4" href="http://dataunion.org/category/events" style="font-size: 14.503937007874pt;" title="29个话题">
        活动
       </a>
       <a class="tag-link-890" href="http://dataunion.org/category/tech/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0" style="font-size: 10.204724409449pt;" title="4个话题">
        深度学习
       </a>
       <a class="tag-link-221" href="http://dataunion.org/category/tech/devl" style="font-size: 18.968503937008pt;" title="193个话题">
        编程语言
       </a>
       <a class="tag-link-888" href="http://dataunion.org/category/career" style="font-size: 15.661417322835pt;" title="48个话题">
        职业规划
       </a>
       <a class="tag-link-5" href="http://dataunion.org/category/jobs" style="font-size: 14.11811023622pt;" title="25个话题">
        职位
       </a>
       <a class="tag-link-871" href="http://dataunion.org/category/industry" style="font-size: 15.716535433071pt;" title="49个话题">
        行业
       </a>
       <a class="tag-link-613" href="http://dataunion.org/category/industry/case" style="font-size: 16.984251968504pt;" title="84个话题">
        行业应用
       </a>
       <a class="tag-link-885" href="http://dataunion.org/category/industry/news" style="font-size: 17.425196850394pt;" title="102个话题">
        行业资讯
       </a>
       <a class="tag-link-10" href="http://dataunion.org/category/training" style="font-size: 14.228346456693pt;" title="26个话题">
        课程
       </a>
       <a class="tag-link-16" href="http://dataunion.org/category/sources" style="font-size: 15.661417322835pt;" title="48个话题">
        资源
       </a>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       功能
      </h4>
      <ul>
       <li>
        <a href="http://dataunion.org/wp-login.php?action=register">
         注册
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/wp-login.php">
         登录
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/feed">
         文章
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/comments/feed">
         评论
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="https://cn.wordpress.org/" title="基于WordPress，一个优美、先进的个人信息发布平台。">
         WordPress.org
        </a>
       </li>
      </ul>
     </div>
    </aside>
    <div class="clear">
    </div>
   </div>
   <!--main-->
   ﻿
   <footer id="dibu">
    <div class="about">
     <div class="right">
      <ul class="menu" id="menu-%e5%ba%95%e9%83%a8%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-18024" id="menu-item-18024">
        <a href="http://dataunion.org/category/partners">
         合作伙伴
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20881" id="menu-item-20881">
        <a href="http://dataunion.org/contribute">
         文章投稿
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20872" id="menu-item-20872">
        <a href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f">
         加入数盟
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22441" id="menu-item-22441">
        <a href="http://dataunion.org/f-links">
         友情链接
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20874" id="menu-item-20874">
        <a href="http://dataunion.org/aboutus">
         关于数盟
        </a>
       </li>
      </ul>
      <p class="banquan">
       数盟社区        ，
        做最棒的数据科学社区
      </p>
     </div>
     <div class="left">
      <ul class="bottomlist">
       <li>
        <a href="http://weibo.com/DataScientistUnion  " target="_blank" 　title="">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weibo.png"/>
        </a>
       </li>
       <li>
        <a class="cd-popup-trigger" href="http://dataunion.org/16787.html#0">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weixin.png"/>
        </a>
       </li>
      </ul>
      <div class="cd-popup">
       <div class="cd-popup-container">
        <h1>
         扫描二维码,加微信公众号
        </h1>
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/2014-12-06-1515289049.png"/>
        <a class="cd-popup-close" href="http://dataunion.org/16787.html">
        </a>
       </div>
       <!-- cd-popup-container -->
      </div>
      <!-- cd-popup -->
     </div>
    </div>
    <!--about-->
    <div class="bottom">
     <a href="http://dataunion.org/">
      数盟社区
     </a>
     <a href="http://www.miitbeian.gov.cn/" rel="external nofollow" target="_blank">
      京ICP备14026740号
     </a>
     联系我们：
     <a href="mailto:contact@dataunion.org" target="_blank">
      contact@dataunion.org
     </a>
     <div class="tongji">
     </div>
     <!--bottom-->
     <div class="scroll" id="scroll" style="display:none;">
      ︿
     </div>
    </div>
   </footer>
   <!--dibu-->
  </div>
 </body>
</html>